package me.sdevil507.systemAssist.systemUserWorkSapce.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.systemAssist.systemUserWorkSapce.dto.SystemUserWorkSpaceDTO;
import me.sdevil507.systemAssist.systemUserWorkSapce.dto.SystemUserWorkSpaceParamsDTO;
import me.sdevil507.systemAssist.systemUserWorkSapce.mapper.SystemUserWorkSpaceMapper;
import me.sdevil507.systemAssist.systemUserWorkSapce.po.SystemUserWorkSpacePO;
import me.sdevil507.systemAssist.systemUserWorkSapce.service.SystemUserWorkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**c
 * @program: tips_back_end
 * @description:
 * @author: kzl
 * @create: 2020-07-02 14:19
 **/
@RestController
@RequestMapping("/api/plat/systemUserWorkSpace")
@Slf4j
public class SystemUserWorkSpaceController {

    @Autowired
    private SystemUserWorkSpaceService systemUserWorkSpaceService;

    @Autowired
    private SystemUserWorkSpaceMapper systemUserWorkSpaceMapper;

    @PostMapping("/v1/readAllByPage")
    public ApiResultDTO getAllByPage(@RequestBody SystemUserWorkSpaceParamsDTO systemUserWorkSpaceParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<SystemUserWorkSpacePO> pagePos = systemUserWorkSpaceService.readAllByPage(systemUserWorkSpaceParamsDTO.getSearchDto(), systemUserWorkSpaceParamsDTO.getPageDto(), systemUserWorkSpaceParamsDTO.getSortDto());
            PageResultDTO<SystemUserWorkSpaceDTO> pageResultDto = (PageResultDTO<SystemUserWorkSpaceDTO>) PageResultGenerator.convert(pagePos, new PageConvertCallBack<SystemUserWorkSpaceDTO, SystemUserWorkSpacePO>() {
                @Override
                public List<SystemUserWorkSpaceDTO> exec(List<SystemUserWorkSpacePO> list) {
                    return  systemUserWorkSpaceMapper.pos2dtos(list);
                }
            });
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pageResultDto);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    @PostMapping("/v1/readAll")
    public ApiResultDTO getAll(@RequestBody SystemUserWorkSpaceDTO systemUserWorkSpaceDTO) {
        ApiResultDTO apiResultDto;
        try {
            systemUserWorkSpaceDTO.setUserId(OrgAccountHelper.getUserId());
            List<SystemUserWorkSpaceDTO> list = systemUserWorkSpaceMapper.pos2dtos(systemUserWorkSpaceService.readAll(systemUserWorkSpaceDTO));
            apiResultDto = ApiResultGenerator.create("0", "获取数据列表成功!", list);
        } catch (Exception e) {
            log.error("获取信息列表失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "获取数据列表失败!");
        }
        return apiResultDto;
    }

    /**
     * 新增systemUserWorkSpace
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO create(@RequestBody SystemUserWorkSpaceDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行新增
        try {
            systemUserWorkSpaceService.create(dto);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("添加失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "新增失败");
        }
        return apiResultDTO;
    }

    /**
     * 删除systemUserWorkSpace
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/delete")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO delete(@RequestBody SystemUserWorkSpaceDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            systemUserWorkSpaceService.delete(dto.getId());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("删除活动失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "删除失败");
        }
        return apiResultDTO;
    }

    /**
     *  彻底删除systemUserWorkSpace
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/clear")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO clearData(@RequestBody SystemUserWorkSpaceDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            systemUserWorkSpaceService.clear(dto.getId());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("删除活动失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "删除失败");
        }
        return apiResultDTO;
    }

}
